//
//  LXCaoZuoTableViewCell.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/12/29.
//

#import "LXCaoZuoTableViewCell.h"
#import "QuanXianModel.h"
#import "HomeCategoryItemCell.h"
#define kItemHeight 70

#define kItemCount 4

@interface LXCaoZuoTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic) UICollectionView *collectionView;

@end


@implementation LXCaoZuoTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    
    if (self) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        flowLayout.itemSize = CGSizeMake(kScreenWidth/kItemCount,kItemHeight);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        flowLayout.minimumLineSpacing= 0;
        flowLayout.minimumInteritemSpacing = 0;
        
        self.collectionView.showsVerticalScrollIndicator = NO;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.backgroundColor = UIColor.whiteColor;
        [self.contentView addSubview:self.collectionView];
        
        self.collectionView.scrollEnabled = NO;
        self.collectionView.frame = CGRectMake(0, 0, kScreenWidth, 30);
        [self.collectionView registerClass:[HomeCategoryItemCell class] forCellWithReuseIdentifier:NSStringFromClass([HomeCategoryItemCell class])];

    }
    return self;
}

- (void)setModel:(QuanXianModel *)model{
    _model = model;

    NSArray *children =  model.children;
    
    CGFloat height = [LXCaoZuoTableViewCell getCellHeightWithDataAry:children];
    self.collectionView.height = height;
    [self.collectionView reloadData];
}



+(CGFloat)getCellHeightWithDataAry:(NSArray*)dataAry
{
    NSInteger totalCount = 0;
    CGFloat sectionHeight = 0;
    for (QuanXianModel *model in dataAry) {
        totalCount += [model.children count];
        sectionHeight += model.height;
    }
    CGFloat height = totalCount/kItemCount *kItemHeight;
    if (totalCount%kItemCount !=0 ){
        height = (totalCount/kItemCount +1) *kItemHeight ;
    }
    return height + sectionHeight;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//
//    QuanXianModel *model = self.dataAry[section];
//    return [model.children count];
//}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeCategoryItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kClsID(HomeCategoryItemCell) forIndexPath:indexPath];
    QuanXianSmallModel *smallModel = self.model.children[indexPath.row];
    cell.titleLb.text = smallModel.name;
    [cell.iconImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",HTTP_URL,[NSString isChinese:smallModel.icon]]] placeholderImage:Image(@"default_icon")];
    return cell;
    
    
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    if (self.itemBlk){
//        self.itemBlk(indexPath);
//    }
}
+(NSString *)reuseIdentifier{
    return @"JingYingTableViewCell";
}

@end


